#pragma once

#include <iostream>
#include <pthread.h>

namespace LockModule
{
    class Mutex
    {
        // 互斥锁: 不支持拷贝构造、拷贝赋值
        Mutex(const Mutex &m) = delete;
        Mutex &operator=(const Mutex &m) = delete;

    public:
        Mutex()
        {
            ::pthread_mutex_init(&_lock, nullptr);
        }

        ~Mutex()
        {
            ::pthread_mutex_destroy(&_lock);
        }

        void Lock()
        {
            ::pthread_mutex_lock(&_lock);
        }

        void Unlock()
        {
            ::pthread_mutex_unlock(&_lock);
        }

    private:
        pthread_mutex_t _lock;
    };

    class LockGuard
    {
    public:
        LockGuard(Mutex &m)
            : _m(m)
        {
            _m.Lock();
        }

        ~LockGuard()
        {
            _m.Unlock();
        }

    private:
        Mutex &_m; // 使用引用: 互斥锁不支持拷贝
    };
}